﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using WpfApplication2.UserControls;
using WpfApplication2.Helpers;
using WpfApplication2.ViewModels.Usuario;


namespace WpfApplication2.Views.Usuario
{
    /// <summary>
    /// Interaction logic for BuscarUsuario.xaml
    /// </summary>
    public partial class BuscarUsuario : Page
    {
        #region Properties

        public Instancias Instancias { get; set; }

        public string Titulo
        {
            get
            {
                return lblTitulo.Content.ToString();
            }
            set
            {
                lblTitulo.Content = value;
            }
        }

        public Visibility BtnEditarVisibility
        {
            get
            {
                return btnEditar.Visibility;
            }
            set
            {
                btnEditar.Visibility = value;
            }
        }

        public Visibility BtnConsultarVisibility
        {
            get
            {
                return btnConsultar.Visibility;
            }
            set
            {
                btnConsultar.Visibility = value;
            }
        }

        public Visibility BtnEliminarVisibility
        {
            get
            {
                return btnEliminar.Visibility;
            }
            set
            {
                btnEliminar.Visibility = value;
            }
        }

        
        private BuscarUsuarioViewModel context
        {
            get
            {
                return (BuscarUsuarioViewModel)DataContext;
            }
        }

        #endregion

        public BuscarUsuario(Instancias instancias, OperacionBusquedaUsuario op)
        {
            InitializeComponent();
            Instancias = instancias;
            if (op == OperacionBusquedaUsuario.Consultar)
            {
                Titulo = "CONSULTA DE USUARIO";
                BtnConsultarVisibility = Visibility.Visible;
                BtnEditarVisibility = Visibility.Collapsed;
                BtnEliminarVisibility = Visibility.Collapsed;
            }
            else if (op == OperacionBusquedaUsuario.Editar)
            {
                Titulo = "EDICION DE USUARIO";
                BtnConsultarVisibility = Visibility.Collapsed;
                BtnEditarVisibility = Visibility.Visible;
                BtnEliminarVisibility = Visibility.Collapsed;
            }
            else
            {
                Titulo = "ELIMINAR USUARIO";
                BtnConsultarVisibility = Visibility.Collapsed;
                BtnEditarVisibility = Visibility.Collapsed;
                BtnEliminarVisibility = Visibility.Visible;
            }
            DataContext = new BuscarUsuarioViewModel(instancias);
        }

        private void EnableDisableButtons(TextBox tb)
        {
            Image img = (tb.Parent as StackPanel).Children[1] as Image;
            if (string.IsNullOrEmpty(tb.Text))
                img.Source = new BitmapImage(new Uri("../../Images/delete_btn.png", UriKind.Relative));
            else
                img.Source = new BitmapImage(new Uri("../../Images/delete_btn_hover.png", UriKind.Relative));
        }

        private void textbox_TextChanged(object sender, TextChangedEventArgs e)
        {
            EnableDisableButtons(sender as TextBox);
        }


        private void BtnLimpiar_MouseDown(object sender, MouseButtonEventArgs e)
        {
            Image img = sender as Image;
            TextBox tb = (img.Parent as StackPanel).Children[0] as TextBox;
            if (tb.Text.Length > 0)
            {
                img.Source = new BitmapImage(new Uri("../../Images/delete_btn.png", UriKind.Relative));
                tb.Text = string.Empty;
            }
        }
    }
}
